package com.source.module_video_record;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.Toast;

/**
 * @author Created by wsl
 * @des
 * @date 2022/6/28
 */
public class VideoRecordActivity extends AppCompatActivity {
    private Camera mCamera;
    private CameraPreview mPreview;

    public static void startActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, VideoRecordActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_record);

        if (checkCameraHardware(this)) {
            Toast.makeText(this, "不支持摄像", Toast.LENGTH_SHORT).show();
//            return;
        }
        // 创建Camera实例
        mCamera = getCameraInstance();

        // 创建预览视图，并作为Activity的内容
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);
    }

    private boolean checkCameraHardware(Context context) {
        // 支持所有版本
        return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
        //  Android 2.3 (API Level 9) 及以上的
        // return  Camera.getNumberOfCameras() > 0;
    }

    public Camera getCameraInstance() {
        Camera c = null;
        try {
            // 在多个摄像头时，默认打开后置摄像头
            c = Camera.open();
            // Android 2.3（API 9之后可指定cameraId摄像头id，可选值为后置（CAMERA_FACING_BACK）/前置（ CAMERA_FACING_FRONT）
            //  c = Camera.open(cameraId);
        } catch (Exception e) {
            // Camera被占用或者设备上没有相机时会崩溃。
        }
        return c;  // returns null if camera is unavailable
    }
}
